package qingxin.servlet.admin.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.JSONArray;
import org.json.JSONObject;

import qingxin.dao.PhotoDAO;
import qingxin.factory.DAOFactory;
import qingxin.vo.Photo;

@WebServlet("/admin/photo/avatarPhotoUploader")
public class AvatarPhotoUploaderServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PhotoDAO photoDAO;
	
	public AvatarPhotoUploaderServlet(){
		photoDAO = DAOFactory.getPhotoDAO();
	}
	
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		    throws ServletException, IOException {
		
		response.setContentType("text/html; charset=utf-8");
    	response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
	
		
		try{
			// Create a factory for disk-based file items
			FileItemFactory factory = new DiskFileItemFactory();
	
			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);
	
			// Parse the request
			List /* FileItem */ items = upload.parseRequest(request);
			
			Iterator iter = items.iterator();
			
			ServletContext ctx = this.getServletContext();
	
			String path = ctx.getRealPath("photos");
		    File file = new File(path);
		    if(!file.exists()){
		    	file.mkdir();
		    }
		    		        
		    JSONArray jsonResult = new JSONArray();
		    
			while (iter.hasNext()) {
			    FileItem item = (FileItem) iter.next();
			    if (item.isFormField()) {
			    	
			    } else {
			    	Photo photo = new Photo();
			    	photoDAO.addAvatar(photo);
			    	
			    	File f = new File(path,photo.getPhotoID()+".jpg");
			    	item.write(f);
			    	
			    	JSONObject jsonPhoto = new JSONObject();
			    	jsonPhoto.put("name", photo.getPhotoID());
			    	
			    	FileInputStream fis = new FileInputStream(f);
			    	jsonPhoto.put("size", fis.available());
			    	
			    	jsonPhoto.put("url", "/qingxinchuanmei/photos/"+photo.getPhotoID()+".jpg");
			    	jsonPhoto.put("thumbnail_url", "/qingxinchuanmei/photos/"+photo.getPhotoID()+".jpg");
			    	jsonPhoto.put("delete_url", "");
			    	jsonPhoto.put("delete_type", "DELETE");
			    	jsonResult.put(jsonPhoto);
			    	
			    }
			}
			
			response.setStatus(HttpServletResponse.SC_OK);
			writer.println(jsonResult.toString());
		}
		catch(Exception e){
			
		}
		finally{
			
		}
	}
}
